package com.yeren.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.sf.json.JSONObject;

import com.yeren.menu.Button;
import com.yeren.menu.ClickButton;
import com.yeren.menu.ComplexButton;
import com.yeren.menu.Menu;
import com.yeren.menu.ViewButton;

/**
 * 菜单操作工具类
 * @author liubiao
 *
 */
public class MenuManagerUtil {
	private static Logger log=LoggerFactory.getLogger(MenuManagerUtil.class);
	//菜单创建（post）
	public final static String menu_create_url="https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN";
	//菜单查询（get）
	public final static String menu_get_url="https://api.weixin.qq.com/cgi-bin/menu/get?access_token=ACCESS_TOKEN";
	//菜单删除（get）
	public final static String menu_delete_url="https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=ACCESS_TOKEN";
	
	
	public static Menu initMenu(){
		//第一个大按钮
				ClickButton btn11=new ClickButton();
				btn11.setName("百度");
				btn11.setType("click");
				btn11.setKey("baidu");
				
				ClickButton btn12=new ClickButton();
				btn12.setName("我维护的公众号");
				btn12.setType("click");
				btn12.setKey("smygzh");
				
				ClickButton btn13=new ClickButton();
				btn13.setName("授权");
				btn13.setType("click");
				btn13.setKey("oauth2");
				
				ClickButton btn14=new ClickButton();
				btn14.setName("js_SDK");
				btn14.setType("click");
				btn14.setKey("js");
				
				ClickButton btn15=new ClickButton();
				btn15.setName("firefly");
				btn15.setType("click");
				btn15.setKey("LTZ");
				
				
				//第二个大按钮
				ViewButton btn21=new ViewButton();
				btn21.setName("上海天意庇哩亚教会");
				btn21.setType("view");
				btn21.setUrl("http://yeren108.duapp.com/weixinty/TyWebsite/ty.jsp");
				
				ViewButton btn22=new ViewButton();
				btn22.setName("boss登陆");
				btn22.setType("view");
				btn22.setUrl("http://yeren108.duapp.com/weixinty/login.jsp");
				
				ViewButton btn23=new ViewButton();
				btn23.setName("斗鱼直播");
				btn23.setType("view");
				btn23.setUrl("http://www.douyu.com/");
				
				ViewButton btn24=new ViewButton();
				btn24.setName("市民云下载");
				btn24.setType("view");         
				btn24.setUrl("http://app.eshimin.com/download.html");
				
				ViewButton btn25=new ViewButton();
				btn25.setName("importnew");
				btn25.setType("view");
				btn25.setUrl("http://www.importnew.com/");
				
				//第三个大按钮
				ViewButton btn31=new ViewButton();
				btn31.setName("市民云");
				btn31.setType("view");
				btn31.setUrl("http://app.eshimin.com");
				
				ViewButton btn32=new ViewButton();
				btn32.setName("市民信箱");
				btn32.setType("view");
				btn32.setUrl("http://www.eshimin.com");
				
				ViewButton btn33=new ViewButton();
				btn33.setName("百度");
				btn33.setType("view");
				btn33.setUrl("http://www.baidu.com");
				
				ViewButton btn34=new ViewButton();
				btn34.setName("新浪新闻");
				btn34.setType("view");
				btn34.setUrl("http://news.sina.com.cn/");
				
				ComplexButton mainBtn1=new ComplexButton();
				mainBtn1.setName("研究");
				mainBtn1.setSub_button(new Button[]{btn11,btn12,btn13,btn14,btn15});
				
				ComplexButton mainBtn2=new ComplexButton();
				mainBtn2.setName("技术交流");
				mainBtn2.setSub_button(new Button[]{btn21,btn22,btn23,btn24,btn25});
				
				ComplexButton mainBtn3=new ComplexButton();
				mainBtn3.setName("其他");
				mainBtn3.setSub_button(new Button[]{btn31,btn32,btn33,btn34});
				
				Menu menu=new Menu();
				menu.setButton(new Button[]{mainBtn1,mainBtn2,mainBtn3});
				
				return menu;
	}
	
	
	
	/**
	 * 创建菜单
	 * 
	 * @param menu 菜单实例
	 * @param accessToken 凭证
	 * @return true | false
	 */
	public static boolean createMenu(Menu menu,String accessToken){
		boolean result=false;
		String url=menu_create_url.replace("ACCESS_TOKEN", accessToken);
		//将菜单对象转换成JSON字符串
		String jsonMenu=JSONObject.fromObject(menu).toString();
		//发起POST请求创建菜单
		JSONObject jsonObject=CommonUtil.httpsRequest(url, "POST", jsonMenu);
		
		if(null!=jsonObject){
			int errorCode=jsonObject.getInt("errcode");
			String errorMsg=jsonObject.getString("errmsg");
			if(0==errorCode){
				result=true;
			}else {
				result=false;
				log.error("创建菜单失败errcode:{ } errmsg:{ }",errorCode,errorMsg);
			}
		}
		return result;
		
	}
	
	/**
	 * 查询菜单
	 * 
	 * @param accessToken
	 * @return String
	 */
	public static String getMenu(String accessToken){
		String result=null;
		String requestUrl=menu_get_url.replace("ACCESS_TOKEN", accessToken);
		//发起GET请求查询菜单
		JSONObject jsonObject=CommonUtil.httpsRequest(requestUrl, "GET", null);
		if(null!=jsonObject){
			result=jsonObject.toString();
		}
		return result;
	}
	
	/**
	 * 删除菜单
	 * 
	 * @param accessToken
	 * @return true | false
	 */
	public static boolean deleteMenu(String accessToken){
		boolean result=false;
		String requestUrl=menu_delete_url.replace("ACCESS_TOKEN", accessToken);
		//发起GET请求删除菜单
		JSONObject jsonObject=CommonUtil.httpsRequest(requestUrl, "GET", null);
		
		if(null!=jsonObject){
			int errorCode=jsonObject.getInt("errcode");
			String errorMsg=jsonObject.getString("errmsg");
			if(0==errorCode){
				result=true;
			}else {
				result=false;
				log.error("删除菜单失败 errcode:( ) errmsg:{ }",errorCode,errorMsg);
			}
		}
		return result;
	}
}
